#compile time options
#OPTS += -DDEBUG_IO #define for some debuggin I/O - none implemented so far
OPTS += -DDEBUG -DDEBUG_LEVEL=1 #leave undefined for no debugging - 0,1, and 2 give progressively more output to stderr
#OPTS += -DTEST_CODE #define to run some basic test code
#OPTS += -DMEMWATCH -DMEMWATCH_STDIO #define to test for memory leaks, out of bounds, etc. for memory used in this code
#OPTS += -DSMALLMEMTEST #define to tell code to just read in part of particles and not to allocate a bunch of memory
#OPTS += -DPERCOLATE_MASS #define to use mass for percolation instead of vcirc
#OPTS += -DLIGHTCONE #define to do halo finding with a light cone - makes sure boundaries are not treated as periodic 
#OPTS += -DSPHERE #define if light cone is centered at 0 with particle coors less than zero - will add half the specified box length to the particle positions
OPTS += -DHAVEFITSIO #set to use FITS binary tables for output, otherwise use ascii
#OPTS += -DDSAMPLEFACTOR=16 #set to a factor by which paticle data will be downsampled
#OPTS += -DPERCOLATEPEAKS # set to percolate peaks wih 1 softening length SO algorithm
OPTS += -DUSESPHSMOOTHPEAKFINDER #set to use an sph kernal smoothing algorithm with 64 nearest neighbors to find peaks
OPTS += -DUSEOPENMP #define to use openmp 
#OPTS += -DPARALLELHALOPERC #define to use a parallel halo percolation - ~10x slower! - however scales in memory use and time with the # of mpi tasks

#select your computer
#COMP="fulla-gcc"
COMP="fulla-icc"
#COMP="mandor-icc"
#COMP="mandor-gcc"

#edit/add to match your machine
ifeq ($(COMP),"fulla-icc")
CC          =  /usr/local/openmpi-intel/bin/mpicc
OPTIMIZE    =  -O3 -ipo #-static -xW
OPENMPFLAG  = -openmp
GSLI        =  
GSLL        =  -lgsl -lgslcblas
FFTWI       =  
FFTWL       =  
FITSI       =  
FITSL       =  -lcfitsio
EXTRACFLAGS =  -I/home/beckermr/include -Wall -wd981 
EXTRACLIB   =  -L/home/beckermr/lib
endif

ifeq ($(COMP),"fulla-gcc")
CC          =  /usr/local/openmpi-gcc/bin/mpicc
OPTIMIZE    =  -O3
OPENMPFLAG  = 
GSLI        =  
GSLL        =  -lgsl -lgslcblas
FFTWI       =  
FFTWL       =  
FITSI       =  
FITSL       =  -lcfitsio
EXTRACFLAGS =  -I/home/beckermr/include -ansi -std=c99 -pedantic -Wall -W -Wmissing-prototypes -Wstrict-prototypes \
	-Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
	-Wwrite-strings -Wnested-externs -fshort-enums -fno-common -Dinline=
EXTRACLIB   =  -L/home/beckermr/lib
endif

ifeq ($(COMP),"mandor-icc")
CC          =  /opt/intel/bin/mpicc 
OPTIMIZE    =  -g -O2
OPENMPFLAG  = 
GSLI        =  -I/home/beckermr/include
GSLL        =  -L/home/beckermr/lib -lgsl -lgslcblas
FFTWI       =  -I/home/beckermr/include
FFTWL       =  -L/home/beckermr/lib -lfftw3 -lfftw3f
FITSI       =  -I/home/beckermr/include
FITSL       =  -L/home/beckermr/lib -lcfitsio
EXTRACFLAGS =  -Wall -wd981 #-wd1419 -wd810
EXTRACLIB   =  
endif

ifeq ($(COMP),"mandor-gcc")
CC          =  mpicc
OPTIMIZE    =  -g #-O3 #-Werror
OPENMPFLAG  = 
GSLI        =  -I/home/beckermr/include
GSLL        =  -L/home/beckermr/lib -lgsl -lgslcblas
FFTWI       =  -I/home/beckermr/include
FFTWL       =  -L/home/beckermr/lib -lfftw3 -lfftw3f
FITSI       =  -I/home/beckermr/include
FITSL       =  -L/home/beckermr/lib -lcfitsio
EXTRACFLAGS =  -ansi -std=c99 -pedantic -Wall -W -Wmissing-prototypes -Wstrict-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align \
	-Wwrite-strings -Wnested-externs -fshort-enums -fno-common -Dinline= 
EXTRACLIB   =  
endif

#set it all up
ifneq (USEOPENMP,$(findstring USEOPENMP,$(OPTS)))
OPENMPFLAG= 
endif

CLINK=$(CC)
CFLAGS=$(OPTIMIZE) $(OPENMPFLAG) $(GSLI) $(FFTWI) $(FITSI) $(EXTRACFLAGS) $(OPTS) 
CLIB=$(EXTRACLIB) $(GSLL) $(FFTWL) $(FITSL) $(GSLL) -lm

ifneq (MEMWATCH,$(findstring MEMWATCH,$(CFLAGS)))
MEMWATCH= 
else
MEMWATCH=memwatch.o
endif

ifneq (TEST_CODE,$(findstring TEST_CODE,$(CFLAGS)))
TESTCODE=
else
TESTCODE=test_code.o
endif

ifneq (HAVEFITSIO,$(findstring HAVEFITSIO,$(CFLAGS)))
FITSIOCODE=
else
FITSIOCODE=peakhalofitsio.o
endif

OBJS = $(MEMWATCH) $(TESTCODE) $(FITSIOCODE) profile.o config.o globalvars.o halofinder.o partio.o domain.o readART.o \
	halofinder_utils.o fortran_io_utils.o peakhaloasciiio.o halodefs.o kdtree.o readGADGET.o kdtree_knnbrs.o peakfinder.o readGADGETsinglefile.o \
	halopercolation_serial.o halopercolation_parallel.o

#halopercolation_parallel.o

EXEC = halofinder
TEST =
all: $(EXEC) clean
test: $(TEST) clean

OBJS1=$(OBJS) main.o
halofinder: $(OBJS1)
	$(CLINK) $(CFLAGS) -o $@ $(OBJS1) $(CLIB)

.PHONY : clean
clean: 
	rm -f *.o

.PHONY : spotless
spotless: 
	rm -f *.o $(EXEC) $(TEST)

